package com.tingyuge.thinking.spring.configuration.bean.definition;

import com.tingyuge.thinking.spring.configuration.bean.factory.DefaultUserFactory;
import com.tingyuge.thinking.spring.configuration.bean.factory.UserFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/27
 */
@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {
        //创建beanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(BeanInitializationDemo.class);
        //启动应用上下文
        applicationContext.refresh();
        // 非延迟初始化在 spring 应用上下文启动完成后,被初始化
        System.out.println("应用已经启动....");
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);
        System.out.println(userFactory);
        System.out.println();
        //关闭应用上下文
        System.out.println("spring 应用上下文准备关闭.....");
        applicationContext.close();
        System.out.println("spring 应用上下文已关闭.....");
    }

//    @Lazy
    @Bean(initMethod = "initUserFactory",destroyMethod = "doDestroy")
    public UserFactory userFactory(){
        return new DefaultUserFactory();
    }
}
